<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<!-- 1、找出最接近的值
/* 尽量不使用 JS 特有的语法糖，尽量不使用如 Array.sort 等语言特有的方法。*/
const arr2 = [1, 9, 5, 15, 28, 55, 33, 78, 99];
/**
 * 返回最接近输入值的数字，如果有多个，返回最大的那个
 * @param {number} n
 * @return {number}
 */
function findNext(n, arr) {
  // your code here...
}

console.log(findNext(1, arr2)); // should print 1
console.log(findNext(44, arr2)); // should print 55
console.log(findNext(6, arr2)); // should print 5
console.log(findNext(7, arr2)); // should print 9
console.log(findNext(8, arr2)); // should print 9 -->

<body>
    <div>test2</div>
    <script>
        const arr2 = [1, 9, 5, 15, 28, 55, 33, 78, 99];
        function findNext(n, arr) {
            let smaller = []
            let larger = []
            let smallermax
            let largermin
            for (let i = 0; i < arr.length; i++) {
                arr[i] >= n ? larger.push(arr[i]) : smaller.push(arr[i])
            }
            smallermax = Math.max(...smaller)
            largermin = Math.min(...larger)
            return largermin - n > n - smallermax ? smallermax : largermin
        }

        console.log(findNext(1, arr2)); // should print 1
        console.log(findNext(44, arr2)); // should print 55
        console.log(findNext(6, arr2)); // should print 5
        console.log(findNext(7, arr2)); // should print 9
        console.log(findNext(8, arr2)); // should print 9
    </script>
</body>

</html>